package com.dx.controller;


import com.dx.annoation.RequiredPermission;
import com.dx.context.ContextHolder;
import com.dx.entity.dto.EmployeeDto;
import com.dx.entity.dto.EmployeeQueryDto;
import com.dx.entity.dto.LoginDto;
import com.dx.result.PageResult;
import com.dx.result.R;
import com.dx.result.Result;
import com.dx.service.AcctivateService;
import com.dx.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController()
//@RestController()不接受路径参数
@RequestMapping("/admin")
@RequiredArgsConstructor
@Slf4j
/**
 * 注意这里用tags标签，swagger生成的目录才会改变信息
 */

@Api(tags = "员工管理接口")
public class EmployeeController {
    //标记为private final且用@RequiredArgsConstructor修饰才有效果
    private final EmployeeService employeeService;
    private final AcctivateService acctivateService;
    @ApiOperation("登录功能")
    @PostMapping("/login")
    public Result<String> login(@RequestBody LoginDto loginDto){
//        EmployeeVo employeeVo = EmployeeVo.builder().name("").state("").build();
        String jwtToken = employeeService.login(loginDto);
        return new Result.Builder<String >().code(Result.SUCCESS).msg("成功").data(jwtToken).build();
    }

    /**
     *
     * @param employeeDto
     * @return
     */
    @ApiOperation("新增员工")
    @PostMapping("/register")
    public Result<EmployeeDto> register(@RequestBody EmployeeDto employeeDto){
        //TODO 实现注册功能
        log.info("新增员工信息{}",employeeDto);
        employeeDto=employeeService.save(employeeDto);
        return new Result.Builder<EmployeeDto>().code(Result.SUCCESS).msg("成功").data(employeeDto).build();
    }

    @RequiredPermission(permissions={"user:add","user:update"})
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    //返回值类型为R<PageResult>
    public R<PageResult> pageQuery(EmployeeQueryDto employeeQueryDto){
        log.info("员工分页查询{}",employeeQueryDto);
        //重命名：鼠标放上去，右键，重构，重命名
        //PageResult<EmployeeDto> PageResult=employeeService.pageQuery(employeeQueryDto);
        PageResult  PageResult=employeeService.pageQuery(employeeQueryDto);
        return R.success("success",PageResult);
    }
//    @PutMapping()
//    @ApiOperation("修改员工状态")
//    public R<?> updateStatus(int id,int status){
//        if(status!= StatsConstant.ENABLE && status!=StatsConstant.DISABLE){
//            throw new ArgumentException("输入了未知状态");
//        }
//        boolean res=employeeService.updateStatus(id,status);
//        return R.success("更新状态成功");
//
//    }

    @RequiredPermission(permissions={"user:add","user:set"})
    @PutMapping("/status/{id}/{status}")
    @ApiOperation("修改员工状态")
    public R<?> updateStatus(@PathVariable("id") Long id, @PathVariable("status") int status){
        boolean res=employeeService.updateStatus(id,status);
        return R.success("更新状态成功");
    }




//    public R<?> get(@RequestParam(value = "name",required) String name, @RequestParam("pageNum") String pageNum, @RequestParam("pageSize") String pageSize){
//
//    }



    @ApiOperation("账号状态获取")
    @GetMapping("/stats")
    public Result<String> getStats(){
        System.out.println("ContextHolder.getUser() = " + ContextHolder.getUser());
        return new Result.Builder<String >().code(Result.SUCCESS).msg("成功").data("状态为："+String.valueOf(ContextHolder.getUser().getStatus())).build();
    }

    @ApiOperation("账号激活与邮箱绑定")
    @GetMapping("/activation")
    public Result<String> activation(@RequestParam("email") String email){
        acctivateService.activation(email);
        return new Result.Builder<String >().code(Result.SUCCESS).msg("成功").data("激活码与激活链接已发送到邮箱，请点击链接或输入激活码进行邮箱绑定").build();
    }
    @ApiOperation("输入收到的账号激活码进行验证")
    @GetMapping("/activation/verify/{uuid}")
    public R<?> activationVerify(@PathVariable("uuid") String uuid){
        acctivateService.activationVerify(uuid);
        return R.success("激活成功");
    }
}
